# 引入路由管理
from logging.config import valid_ident
from fastapi import APIRouter,File,UploadFile
# 让数据以json的格式返回
from fastapi.responses import JSONResponse
from func import isSN,readMsg,pictureIn,dataIn,selectStudentById,genderRatio,collegeCoverageRate
from func import sumOfStu,fromplaceRatio,ageDistribution,collegeDistribution,genderCount


# 构建api路由
router = APIRouter()
#登录接口
@router.post("/loginService")
async def loginService(s:dataIn.Student):
    judge=False
    flag = selectStudentById.selectStudentById(s.stuid)
    if flag != []:
        judge=True
        return judge
    else :
        return judge
#注册接口
@router.post("/createStu")
async def createStu(s:dataIn.Student):
    flag = dataIn.dataIn(s)
    if flag == 1:
        return s.stuName + "已报到"
    elif flag == -1:
        return JSONResponse(
                content={
                    "code" : flag,
                    "Name" : s.stuName,
                    "msg" : "id已存在，注册失败"
                }
            )  

#上传图片的接口


@router.post('/pictureIn')
# 函数参数即为前端传递过来的文件/字符串
# 1.接收前端上传的文件
async def pictureIn(
        fileb: UploadFile = File(...),      # UploadFile转为文件对象
        sid: str = ""
):
    # 2.读取前端上传到的fileb文件
    imgbytes = await fileb.read()
    # print(contents)
    # print(fileb.filename)
    img_url = 'http://rglp5by35.hn-bkt.clouddn.com/' + \
        pictureIn.pictureIn(imgbytes, sid)
    return (img_url)

#生源地分布
@router.post("/fromplaceRa")
async def fromplaceRa():
    return fromplaceRatio.fromplaceRatio()
#年龄分布
@router.post("/ageDistribu")
async def ageDistribu():
    data = ageDistribution.ageDistribution()
    return data
#学院分布
@router.post("/collegeDistribu")
async def collegeDistribu():
    return collegeDistribution.collegeDistribution()
#性别分布比率
@router.post("/genderRa")
async def genderRa():
    sum = collegeCoverageRate.collegeCoverageRate()
    data = genderRatio.genderRatio(sum)
    return  [data]
#性别人数
@router.post("/genderC")
async def genderC():
    data = genderCount.genderCount()
    return  [data]
#总人数
@router.post("/sumOf")
async def sumOf():
    return [{
        "总人数":sumOfStu.sumOfStu()
        }]
#注册人数
@router.post("/collegeCoverage")
async def collegeCoverage():
    sum = sumOfStu.sumOfStu()
    return [{
        "已报到人数":collegeCoverageRate.collegeCoverageRate(),
        "应报到人数":sum
        }]
#读取信息
@router.post("/readM")
async def readM(s:dataIn.Student):
    return readMsg.readMsg(s)

#判定省内外
@router.post("/isS")
async def isS():
    sumR = collegeCoverageRate.collegeCoverageRate()
    return isSN.isSN(sumR)